package main

import (
	"flag"
	"fmt"
	"github.com/spf13/viper"
	"log"
	"pim/pkg/config"
	"pim/pkg/im_server"
)

var configPath = flag.String("c", "./config.yaml", "配置文件")

func main() {
	flag.Parse()

	config, err := NewConfig(*configPath)
	if err != nil {
		panic(err)
	}
	//config := &im_server.Config{}
	ps := im_server.AppNew(config)
	err = ps.Run()
	if err != nil {
		fmt.Println(err)
	}
}

func NewConfig(path string) (*config.ImServerConfig, error) {
	v := viper.New()
	v.AddConfigPath(".")
	//v.SetConfigType("yaml")
	v.SetConfigFile(path)
	//v.SetConfigName("config")
	err := v.ReadInConfig()
	if err != nil {
		return nil, err

	}
	config := new(config.ImServerConfig)

	err = v.UnmarshalKey("pim", config)

	if err != nil {
		//log.Fatalln("配置文件错误 ", err)
		return nil, err
	}

	if config.HttpPort == 0 {
		config.HttpPort = 10004
	}

	if config.RedisIP == "" {
		config.RedisIP = "127.0.0.1:6379"
	}

	if config.DBUri == "" {
		log.Fatalln("db_uri 未配置")
	}

	return config, nil
}
